<?php


namespace app\common\model;

use app\common\model\Device;
use app\common\model\Park;
use think\Model;
use think\Db;

class Station extends Model
{
    protected $table = "store_power_station";

    public function typeof($lists)
    {
        foreach ($lists as $k => &$v) {
            if ($v['type'] == 1) {
                $v['type_name'] = '物业';
            }
            if ($v['type'] == 2) {
                $v['type_name'] = '个人';
            }
            if ($v['type'] == 3) {
                $v['type_name'] = '联营电站';
            }
            if ($v['type'] == 4) {
                $v['type_name'] = '非联营电站';
            }
        }
        return $lists;
    }


    public function lists($param)
    {
        $lat = $param['lat'] ?? "0";
        $lng = $param['lng'] ?? "0";
        $type = $param['type'] ?? "";
        $is_park = $param['is_park'] ?? "";
        $is_power = $param['is_power'] ?? "";
        $power_rate = $param['power_rate'] ?? "";
        $speed = $param['speed'] ?? "";

        $where = "where name is not null and status=1 and delete_time is null and is_park=1";
        if ($type) {
            if ($type != '请选择') {
                $where .= " and type='$type'";
            }
        }
        if ($is_park) {
            if ($is_park != '请选择') {
                $where .= " and is_park='$is_park'";
            }
        }
        if ($is_power) {
            if ($is_power != '请选择') {
                $where .= " and is_power='$is_power'";
            }
        }
        if ($speed) {
            if ($speed != '请选择') {
                $arr = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['speed'=>$speed,'status'=>1])->field("station_id")->select();
                $ids = "";
                foreach ($arr as $kk =>&$vv){
                    $ids .= $vv['station_id'].',';
                }
                $ids = trim($ids,',');
                $where .= " and id in ($ids)";
            }
        }
        if ($power_rate) {
            if ($power_rate != '请选择') {
                $power_rate = str_replace('kw','',$power_rate);
                $where .= " and power_rate like '%$power_rate%'";
            }
        }

        $sql = "SELECT s.*,(6371 * acos (cos ( radians($lat) ) * cos( radians( s.lat ) ) * cos( radians( s.lng ) - radians($lng) ) 
+ sin ( radians($lat) ) * sin( radians( s.lat ) ) ) ) AS distance FROM $this->table s $where ORDER BY distance";

        $lists = Db::query($sql);
        $arr = [
            '0:00~0:30','0:30~1:00','1:00~1:30','1:30~2:00','2:00~2:30','2:30~3:00','3:00~3:30','3:30~4:00','4:00~4:30','4:30~5:00',
            '5:00~5:30','5:30~6:00','6:00~6:30','6:30~7:00','7:00~7:30','7:30~8:00','8:00~8:30','8:30~9:00',
            '9:00~9:30','9:30~10:00','10:00~10:30','10:30~11:00','11:00~11:30','11:30~12:00','12:00~12:30','12:30~13:00',
            '13:00~13:30','13:30~14:00','14:00~14:30','14:30~15:00','15:00~15:30','15:30~16:00','16:00~16:30','16:30~17:00',
            '17:00~17:30','17:30~18:00','18:00~18:30','18:30~19:00','19:00~19:30','19:30~20:00','20:00~20:30','20:30~21:00',
            '21:00~21:30','21:30~22:00','22:00~22:30','22:30~23:00','23:00~23:30','23:30~24:00'
        ];
        foreach ($lists as $k => &$v) {
            $v['range'] = sprintf('%.2f', ($v['distance']));
            if($v['range']>999){
                $v['range'] = 999;
            }else{
                $v['range'] = sprintf('%.2f',($v['distance']));
            }

            if ($v['type'] == 1) {
                $v['type_name'] = '物业';
            }
            if ($v['type'] == 2) {
                $v['type_name'] = '个人';
            }
            if ($v['type'] == 3) {
                $v['type_name'] = '联营电站';
            }
            if ($v['type'] == 4) {
                $v['type_name'] = '非联营电站';
            }

            $v['open_name'] = $v['charge_name'] = '';

            if ($v['is_open'] == 1) {
                $v['open_name'] = '对外开放';
            }

            if ($v['is_charge'] == 1) {
                $v['charge_name'] = '停车收费';
            }

            $v['kuai_total'] = Db::table("store_device")->where("is_shenhe=1")->where(["station_id"=>$v['id']])->where("delete_time is null and speed='快'")->count();
            $v['kuai_status'] = Db::table("store_device")->where("is_shenhe=1")->where(["station_id"=>$v['id']])->where("delete_time is null and speed='快' and status=1")->count();
            $v['man_total'] = Db::table("store_device")->where("is_shenhe=1")->where(["station_id"=>$v['id']])->where("delete_time is null and speed='慢'")->count();
            $v['man_status'] = Db::table("store_device")->where("is_shenhe=1")->where(["station_id"=>$v['id']])->where("delete_time is null and speed='慢' and status=1")->count();


            $v['device'] = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(["station_id"=>$v['id']])->order('price asc')->find();
            if($v['device']){
                $v['device']['park'] = Db::table("store_park")->where(['id'=>$v['device']['park_id']])->find();

                $v['cost_price1'] = '尖电价：'.sprintf('%.4f',$v['device']['price']).'元/度 <br>服务费：'.sprintf('%.4f',$v['device']['service_price']).'元/度 <br>';
                $cost_time1 = '';
                $v['cost_price2'] = '峰电价：'.sprintf('%.4f',$v['device']['price2']).'元/度 <br>服务费：'.sprintf('%.4f',$v['device']['service_price2']).'元/度 <br>';
                $cost_time2 = '';
                $v['cost_price3'] = '平电价：'.sprintf('%.4f',$v['device']['price3']).'元/度 <br>服务费：'.sprintf('%.4f',$v['device']['service_price3']).'元/度 <br>';
                $cost_time3 = '';
                $v['cost_price4'] = '谷电价：'.sprintf('%.4f',$v['device']['price4']).'元/度 <br>服务费：'.sprintf('%.4f',$v['device']['service_price4']).'元/度 <br>';
                $cost_time4 = '';

                $cost_model = json_decode($v['device']['cost_model'],true);
                if($cost_model){
                    foreach ($cost_model as $kk =>&$vv){
                        if($vv==1){
                            $cost_time1 .= $arr[$kk].' ';
                        }
                        if($vv==2){
                            $cost_time2 .= $arr[$kk].' ';
                        }
                        if($vv==3){
                            $cost_time3 .= $arr[$kk].' ';
                        }
                        if($vv==4){
                            $cost_time4 .= $arr[$kk].' ';
                        }
                    }
                    $v['cost_price1'] .= ' 时段：'.$cost_time1;
                    $v['cost_price2'] .= ' 时段：'.$cost_time2;
                    $v['cost_price3'] .= ' 时段：'.$cost_time3;
                    $v['cost_price4'] .= ' 时段：'.$cost_time4;
                }else{
                    $v['cost_price1'] = ' ';
                    $v['cost_price2'] = ' ';
                    $v['cost_price3'] = ' ';
                    $v['cost_price4'] = ' ';
                }


            }else{
                $v['device']['park'] = [];
                $v['cost_price1'] = ' ';
                $v['cost_price2'] = ' ';
                $v['cost_price3'] = ' ';
                $v['cost_price4'] = ' ';
            }

        }
        return $lists;
    }


    public function info($param)
    {
        $id = $param['id'] ?? "";
        if ($id) {
            $list = self::where("id=$id")->find();
            $list['images'] = str_replace('|',',',$list['images']);
            $Device = new Device();
            $Park = new Park();

            $date = date('Y-m-d H:i:s');
            $arr = [];
            $device = $Device::where(["station_id" => $list['id']])->where("is_shenhe=1 and delete_time is null")->select();
            foreach ($device as $k =>&$v){
                $v['price'] = sprintf('%.4f',0);

                $total_price = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('price')+Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('service_price');
                $total_price2 = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('price2')+Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('service_price2');
                $total_price3 = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('price3')+Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('service_price3');
                $total_price4 = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('price4')+Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->sum('service_price4');

                $total = sprintf('%.5f', (($total_price + $total_price2 + $total_price3 + $total_price4) / 4));
                if ($total) {
                    $count = Db::table("store_device")->where("is_shenhe=1 and delete_time is null")->where(['station_id' => $id])->count();
                    if ($count) {
                        $v['price'] = sprintf('%.4f', $total / $count);
                    }
                }



                $v['park'] = Db::table("store_park")->where(['id'=>$v['park_id']])->find();

                $v['flag'] = 0;
                $start_time = date('Y-m-d').' '.$v['start_time'].'00';
                $end_time = date('Y-m-d').' '.$v['end_time'].'00';

                $start_time2 = date('Y-m-d').' '.$v['start_time2'].'00';
                $end_time2 = date('Y-m-d').' '.$v['end_time2'].'00';

                $start_time3 = date('Y-m-d').' '.$v['start_time3'].'00';
                $end_time3 = date('Y-m-d').' '.$v['end_time3'].'00';

                if($v['start_time']&&$v['end_time']){
                    if($date>=$start_time){
                        if($date<=$end_time){
                            $v['flag'] = 1;
                        }
                    }
                }
                if($v['start_time2']&&$v['end_time2']){
                    if($date>=$start_time2){
                        if($date<=$end_time2){
                            $v['flag'] = 1;
                        }
                    }
                }
                if($v['start_time3']&&$v['end_time3']){
                    if($date>=$start_time3){
                        if($date<=$end_time3){
                            $v['flag'] = 1;
                        }
                    }
                }

                //if($v['flag']){
                    array_push($arr,$v);
                //}
            }
            $list['device'] = $arr;
            $list['device_count'] = count($list['device']);


            $arr2 = [];
            $park_list = $Park::where(["station_id" => $list['id']])->where("delete_time is null")->select();
            foreach ($park_list as $k =>&$v){
                $v['flag'] = 0;
                $start_time = date('Y-m-d').' '.$v['start_time'].'00';
                $end_time = date('Y-m-d').' '.$v['end_time'].'00';

                $start_time2 = date('Y-m-d').' '.$v['start_time2'].'00';
                $end_time2 = date('Y-m-d').' '.$v['end_time2'].'00';

                $start_time3 = date('Y-m-d').' '.$v['start_time3'].'00';
                $end_time3 = date('Y-m-d').' '.$v['end_time3'].'00';

                if($v['start_time']&&$v['end_time']){
                    if($date>=$start_time){
                        if($date<=$end_time){
                            $v['flag'] = 1;
                        }
                    }
                }
                if($v['start_time2']&&$v['end_time2']){
                    if($date>=$start_time2){
                        if($date<=$end_time2){
                            $v['flag'] = 1;
                        }
                    }
                }
                if($v['start_time3']&&$v['end_time3']){
                    if($date>=$start_time3){
                        if($date<=$end_time3){
                            $v['flag'] = 1;
                        }
                    }
                }

                //if($v['flag']){
                    array_push($arr2,$v);
                //}
            }


            $list['park'] = $arr2;
            $list['park_count'] = count($list['park']);
            return $list;
        } else {
            return '';
        }
    }


    public function add($param)
    {
        $row = self::where(["user_id" => $param['user_id']])->find();
        if (!empty($row)) {
            return '您已添加电站了,请勿重复提交！';
        } else {
            $find = self::where(["name" => $param['name'], 'phone' => $param['phone']])->find();
            if (empty($find)) {
                $param['price'] = 0;
                $param['speed'] = '快';
                $param['create_at'] = date('Y-m-d H:i:s');
                $param['images'] = str_replace(',','|',$param['images']);
                self::insert($param);

                Db::table("store_member")->where(["id" => $param['user_id']])->update(['is_have_station'=>1]);
                return 1;
            } else {
                return '手机号或名称重复,请勿重复提交！';
            }
        }

    }


    public function station_save($param)
    {
        $user_id = $param['user_id'];
        $find = self::where(['phone' => $param['phone']])->where("user_id!=$user_id")->find();
        if (empty($find)) {
            $row = self::where(["id" => $param['id']])->find();
            if ($row) {
                if ($row['user_id'] == $param['user_id']) {
                    $param['create_at'] = date('Y-m-d H:i:s');
                    $param['images'] = str_replace(',','|',$param['images']);
                    self::where(["id" => $param['id']])->update($param);
                    Db::table("store_member")->where(["id" => $param['user_id']])->update(['is_have_station'=>1]);

                    $res = self::where(["id" => $param['id']])->find();
                    $lat = $res['lat'];
                    $lng = $res['lng'];
                    Db::table("store_park")->where(['station_id'=>$param['id']])->update(['lat'=>$lat,'lng'=>$lng]);
                    Db::table("store_device")->where(['station_id'=>$param['id']])->update(['lat'=>$lat,'lng'=>$lng]);

                    return 1;
                } else {
                    return '这不是您的电站,请勿提交！';
                }
            } else {
                return '电站未找到！';
            }
        } else {
            return '手机号重复,请勿重复提交！';
        }
    }

    public function del($param)
    {
        $find = self::where(['id' => $param['id']])->where(["user_id" => $param['user_id']])->find();
        if (empty($find)) {
            return '电站未找到！';
        } else {
            self::where(['id' => $param['id']])->where(["user_id" => $param['user_id']])->update(['delete_time' => date('Y-m-d H:i:s')]);
            return 1;
        }
    }

}